<?php
namespace app\index\model;
use Ip2Region\Ip2Region;
use think\Model;
class IpRegion extends Model{
    public function getRegionByIp($line){
        if ( ip2long($line) == NULL ) {
            return "IP地址不正确";
        }
        $method     = 'btreeSearch';
        $algorithm  = 'B-tree';
        $ip2regionObj = new Ip2Region(false);
        $s_time = $this->getTime();
        $data   = $ip2regionObj->{$method}($line);
        $c_time = $this->getTime() - $s_time;
        return "{$data['city_id']}|---|{$data['region']} in {$c_time} millseconds";
    }

    public function getTime()
    {
        return (microtime(true) * 1000);
    }
}
